package com.googlecode.homework.qlr.zk;

import java.io.IOException;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;

public class ZookeeperTest {
	public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
		
		ZooKeeper zk = new ZooKeeper("localhost:2181",1000,new Watcher(){
			@Override
			public void process(WatchedEvent event) {
				System.out.println("get new Event:"+event);
			}
		});
		
		if(zk.exists("/roottest/child", false)!=null){
			zk.delete("/roottest/child", -1);
		}
		
		if(zk.exists("/roottest", false)!=null){
			zk.delete("/roottest", -1);
		}
		
		System.out.println("create:"+zk.create("/roottest", "qiulirun".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT));
		
		zk.create("/roottest/child", "qiulirun-child".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
		
		System.out.println(new String(zk.getData("/roottest", false, null)));
		
		System.out.println(zk.getChildren("/roottest", true));
		
		zk.setData("/roottest", "modified-qiulirun".getBytes(), -1);
		
		
		
		zk.close();
	}
}
